using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using SqlSugar;
using System.Web.Http;
using wnet.admin.dbhelper;
using wnet.admin.Models;
using System;
using Models;
using wnet.admin.Models.dto;
using wnet.admin.wentcommon;
using System.Collections.Generic;


namespace wnet.admin.Controllers.ywBusiness
{
    /// <summary>
    /// 数据统计
    /// </summary>
    //[TokenAuth]
    [Route("ywbusiness/homedata/{action}")]
    public class homedataController : ApiController
    {

        private readonly ISqlSugarClient _sqlSugarClient;
        public homedataController()
        {
            _sqlSugarClient = SqlSugarConfig.CreateClient();
        }
      




        /// <summary>
        /// 首页统计--数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object getstatisticsdata()
        {
            LogHelper.WriteLog("哈哈哈");
            var yw_statistics_scarinfo = _sqlSugarClient.Queryable<yw_statistics_scar>().First() ?? new yw_statistics_scar();
            var yw_statistics_numinfo = _sqlSugarClient.Queryable<yw_statistics_num>().First() ?? new yw_statistics_num();
            var yw_statistics_stateinfo = _sqlSugarClient.Queryable<yw_statistics_state>().First() ?? new yw_statistics_state();
            if (string.IsNullOrWhiteSpace(yw_statistics_stateinfo.state))
            {
                yw_statistics_stateinfo.state = "停用";
            }
            var data = new
            {
                code = 200,
                msg = "成功",
                data = new
                {
                    scarnum = yw_statistics_scarinfo.scarnum,//伤痕数量
                    alarmnum = yw_statistics_numinfo.alarmnum,//报警条件
                    measurelength = yw_statistics_numinfo.measurelength,//测量总长
                    state = yw_statistics_stateinfo.state//当前状态
                }
            };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }
        /// <summary>
        /// 首页统计 --小时伤痕趋势图
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [HttpGet]
        public object getstatisticsscar(string day = "")
        {
            DateTime time = DateTime.Now;
            var db = _sqlSugarClient;

            string sqlstr = @"SELECT strftime('%H', alarmdate) AS hour, COUNT(*) AS num
FROM yw_llarmlist
WHERE alarmdate >= DATETIME('2024-11-04', '-24 hours')
GROUP BY hour
ORDER BY hour;";
            sqlstr = @"SELECT 
    HOUR(alarmdate) as `hour`,
    COUNT(*) as `num`
FROM 
    yw_llarmlist
WHERE 
    alarmdate > NOW() - INTERVAL 24 HOUR
GROUP BY 
    HOUR(alarmdate)
		ORDER BY   HOUR(alarmdate)";
            var dlist = _sqlSugarClient.Ado.SqlQuery<sqlscardto>(sqlstr);
            foreach (var d in dlist)
            {
                d.hournum = Convert.ToInt32(d.hour) + 1;
            }

            var data = new
            {
                code = 200,
                msg = "成功",
                data = dlist,

            };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }
        /// <summary>
        /// 首页统计 --小时探伤报警信息
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [HttpGet]
        public object getstatisticstanshangbaojing(string day = "")
        {
            DateTime time = DateTime.Now;
            var db = _sqlSugarClient;

            string sqlstr = @"SELECT 
    HOUR(alarmdate) as `hour`,
    COUNT(*) as `num`
FROM 
    yw_llarmlist
WHERE 
    alarmdate > NOW() - INTERVAL 24 HOUR
GROUP BY 
    HOUR(alarmdate)
		ORDER BY   HOUR(alarmdate)";
            var dlist = _sqlSugarClient.Ado.SqlQuery<sqlscardto>(sqlstr);
            foreach (var d in dlist)
            {
                d.hournum = Convert.ToInt32(d.hour) + 1;
            }

            var data = new
            {
                code = 200,
                msg = "成功",
                data = dlist,

            };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }


        /// <summary>
        /// 首页统计--报警信息
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public object gettablellarmtoplist()
        {
            PagerInfo parm = new PagerInfo();
            parm.Sort = "id";
            parm.SortType = "desc";
            parm.PageNum = 1;
            parm.PageSize = 8;
            var dlist = _sqlSugarClient.Queryable<yw_llarmlist>().ToPage<yw_llarmlist>(parm);

            var data = new
            {
                code = 200,
                msg = "成功",
                data = dlist,

            };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }


        /// <summary>
        /// 首页-头部--报警信息
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public object getheadllarmtoplist()
        {
            PagerInfo parm = new PagerInfo();
            parm.Sort = "id";
            parm.SortType = "desc";
            parm.PageNum = 1;
            parm.PageSize = 4;
            var dlist = _sqlSugarClient.Queryable<yw_llarmlist>().ToPage<yw_llarmlist>(parm);

            var data = new
            {
                code = 200,
                msg = "成功",
                data = dlist,

            };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }


        /// <summary>
        /// 首页-头部--通知
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public object getheadnoticelist()
        {
            PagerInfo parm = new PagerInfo();
            parm.Sort = "id";
            parm.SortType = "desc";
            parm.PageNum = 1;
            parm.PageSize = 4;
            var dlist = _sqlSugarClient.Queryable<yw_notice>().Where(q => q.states == 1).ToPage<yw_notice>(parm);

            var data = new
            {
                code = 200,
                msg = "成功",
                data = dlist,

            };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }


        /// <summary>
        /// 首页统计--信息列表
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public object gettopnewsllist()
        {
            PagerInfo parm = new PagerInfo();
            parm.Sort = "id";
            parm.SortType = "desc";
            parm.PageNum = 1;
            parm.PageSize = 4;
            var dlist = _sqlSugarClient.Queryable<yw_news>().ToPage<yw_news>(parm);

            var data = new
            {
                code = 200,
                msg = "成功",
                data = dlist,

            };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 首页统计--任务列表
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public object gettoptasklist()
        {
            PagerInfo parm = new PagerInfo();
            parm.Sort = "id";
            parm.SortType = "desc";
            parm.PageNum = 1;
            parm.PageSize = 4;
            var dlist = _sqlSugarClient.Queryable<yw_taskinfo>().ToPage<yw_taskinfo>(parm);

            var data = new
            {
                code = 200,
                msg = "成功",
                data = dlist,

            };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 首页统计--任务列表-快速添加
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public object addtaskinfo(string title)
        {
            yw_taskinfo taskinfo = new yw_taskinfo();
            taskinfo.title = title;
            taskinfo.detailinfo = title;
            taskinfo.createtime = DateTime.Now;
            taskinfo.updatetime = DateTime.Now;

            var response = _sqlSugarClient.Insertable<yw_taskinfo>(taskinfo).ExecuteCommand();
            var data = new
            {
                code = 200,
                msg = "成功",
                data = response,

            };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 首页统计--任务列表-快速删除
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public object taskdel(int id)
        {

            var response = _sqlSugarClient.Queryable<yw_taskinfo>()
                    .Where(q => q.id == id).First();

            _sqlSugarClient.Deleteable<yw_taskinfo>(response).ExecuteCommand();
            var data = new
            {
                code = 200,
                msg = "删除成功",
                data = new { },

            };
            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }
    }
}